﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.Net.Mail;
using System.Data;

namespace Site.Secure
{
    public partial class Add_User : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (Session["userType"].ToString() == "admin" || Session["userType"].ToString() == "root") //If the current user is a admin allow them to continue
                {

                    if (edtUser.Text.Length != 0 && edtPass.Text != "" && edtEmail.Text != "") //Make sure the data is not null
                    {

                        SqlDataSource1.InsertCommand = "INSERT INTO Users (userID, userPass, userType, userHidden, userEmail) VALUES('" + edtUser.Text + "', '" + edtPass.Text + "', '" + DropDownList1.Text + "', 'False', '" + edtEmail.Text + "')";
                        SqlDataSource1.Insert();
                        Logger.Log("The User '" + Session["userID"] + "' Created the user '" + edtUser.Text + "'", "USER CREATED", Server);
                         

                        //Send email to new user and send email to all existing users to inform about newly created account
                        var smtp = new SmtpClient
                        {
                            Host = "africanhoneybee.net",
                            Port = 26,
                            EnableSsl = false,
                            DeliveryMethod = SmtpDeliveryMethod.Network,
                            UseDefaultCredentials = false,
                            Credentials = new NetworkCredential("noreply@africanhoneybee.net", "857105")
                        };

                        MailMessage message = new MailMessage();
                        message.From = new MailAddress("noreply@africanhoneybee.net", "AHB Webmaster");
                        message.To.Add(edtEmail.Text);
                        message.Subject = "AHB Account Registration";
                        message.Body = "Your AHB Data Portal Account has been activated, your username is '" + edtUser.Text + "' and your password is '" + edtPass.Text + "'";
                        smtp.Send(message);

                        SqlDataSource1.SelectCommand = "SELECT * FROM Users";
                        DataView DV = (DataView)SqlDataSource1.Select(new DataSourceSelectArguments());


                        foreach (DataRowView DRV in DV)
                        {
                            if (DRV["userEmail"].ToString() != null && DRV["userEmail"].ToString() != "")
                            {

                                message.From = new MailAddress("noreply@africanhoneybee.net", "AHB Webmaster");
                                message.To.Add(DRV["userEmail"].ToString());
                                message.Subject = "AHB User Create Notification";
                                message.Body = "The User '" + edtUser.Text + "' has been created on the AHB Data Portal";
                                smtp.Send(message);
                            }
                        }


                        Response.Redirect("Default.aspx");
                    }
                    else
                        System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('Please fill in all the required details')</SCRIPT>");
                }
                else
                {
                    System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert('You are not allowed to add users, please login with an account that has full root access')</SCRIPT>");
                }
            }
            catch
            {
                //If you get here it means that Session == NULL, requires user to re-login
                System.Web.HttpContext.Current.Response.Write("<SCRIPT LANGUAGE='JavaScript'>alert(Your session has expired, this means that you can not perform any root tasks on this website. Please login again or restart your browser and navigate back to this page.')</SCRIPT>");
            }
        }
    }
}